//
//  RegisterInfoController.m
//  PersonnelWashCar
//
//  Created by 张越 on 16/4/29.
//  Copyright © 2016年 张越. All rights reserved.
//

#import "RegisterInfoController.h"
#import "WaitAssessViewController.h"

@interface RegisterInfoController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) IBOutlet UITextField *sexTextField;
@property (strong, nonatomic) IBOutlet UITextField *ageTextField;
@property (strong, nonatomic) IBOutlet UITextField *areaTextField;
@property (strong, nonatomic) IBOutlet UITextField *addressTextField;

@end

@implementation RegisterInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"注册信息";
    [self addBackButton];
    self.image1.layer.cornerRadius = self.image2.layer.cornerRadius = self.image3.layer.cornerRadius = self.image4.layer.cornerRadius = self.image5.layer.cornerRadius = self.finshButton.layer.cornerRadius = 5.0f;
    self.image1.layer.masksToBounds = self.image2.layer.masksToBounds = self.image3.layer.masksToBounds = self.image4.layer.masksToBounds = self.image5.layer.masksToBounds = self.finshButton.layer.masksToBounds = YES;
    
    self.ageTextField.delegate = self;
    self.areaTextField.delegate = self;
    self.addressTextField.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

// 完成按钮
- (IBAction)finshButtonAction:(UIButton *)sender {
    if ([self.nameTextField.text isEqual:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名！"];
    }else if ([self.sexTextField.text isEqual:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入性别！"];
    }
    else if ([self.ageTextField.text isEqual:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入年龄！"];
    }else if ([self.areaTextField.text isEqual:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入地区！"];
    }
    else if ([self.addressTextField.text isEqual:@""])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入地址！"];
    }else
    {
        [self webRequest_UserInfoName:self.nameTextField.text userSex:self.sexTextField.text userAge:self.ageTextField.text userArea:self.areaTextField.text userAddress:self.addressTextField.text];
    }
}

- (void)webRequest_UserInfoName:(NSString *)username userSex:(NSString *)sex userAge:(NSString *)age userArea:(NSString *)area userAddress:(NSString *)address
{
    [self.nameTextField resignFirstResponder];
    [self.sexTextField resignFirstResponder];
    [self.ageTextField resignFirstResponder];
    [self.areaTextField resignFirstResponder];
    [self.addressTextField resignFirstResponder];
    NSString *uidStr = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserIdKey];
    NSDictionary *dict =  @{@"uid":uidStr,@"nickname":username,@"sex":sex,@"age":age,@"city":area,@"address":address};
    [HttpRequestManager getMethod:KRegisterUserInfoInterface parameters:dict completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            
            WaitAssessViewController *waitVC = [[WaitAssessViewController alloc]init];
            [self.navigationController pushViewController:waitVC animated:YES];
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"注册失败"];
        }
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.nameTextField resignFirstResponder];
    [self.sexTextField resignFirstResponder];
    [self.areaTextField resignFirstResponder];
    [self.addressTextField resignFirstResponder];
    [self.ageTextField resignFirstResponder];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [self changeViewFrameWithKeyBoardNotification:notification isShow:NO];
}

- (void)changeViewFrameWithKeyBoardNotification:(NSNotification *)notification isShow:(BOOL)isShow
{
    NSDictionary *userInfo = [notification userInfo];
    NSNumber *animationDur = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCur = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    [UIView beginAnimations:@"changeViewFrame" context:nil];
    [UIView setAnimationCurve:[animationCur integerValue]];
    [UIView setAnimationDuration:[animationDur doubleValue]];
    [self.view setFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [UIView commitAnimations];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    if (ScreenHeight == 480) {
        //上移n个单位，按实际情况设置
        CGRect rect=CGRectMake(0.0f,-70,width,height);
        self.view.frame=rect;
        [UIView commitAnimations];
        
    } else if (ScreenHeight == 568) {
        //上移n个单位，按实际情况设置
        CGRect rect=CGRectMake(0.0f,-30,width,height);
        self.view.frame=rect;
        [UIView commitAnimations];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
